/**
 * Lockbox
 * a Password and Secret Information Manager
 * View to display a table of LockboxItems
 *
 * Created by Nathan Black
 */

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <GraphicsServices/GraphicsServices.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIAlertSheet.h>
#import "LockboxPasswordView.h"


@interface LockboxPasswordView (Private)

-(void)pwViewFinished;

@end

@implementation LockboxPasswordView

- (void)dealloc
{
    [_background release];
    [_pwPrompt release];

    [super dealloc];
}

///////////// Constructor ////////////////
- (id)initWithFrame:(struct CGRect)frame storage:(LockboxStorage*)storage app:(id)app
{
    self = [super initWithFrame:frame];
    if (self)
    {
        if (storage == NULL || app == NULL) {
            NSLog(@"LockboxPasswordView : invalid params to initWithFrame");
            return NULL;
        }
        _storage = storage;
        _delegate = app;
        
        int state = ([_storage exists] ? LBPP_AUTH : LBPP_NEW);
        NSLog(@"state is %d", state);
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        
        // Allocate the background, but scoot it up a bit, so it alings with the transform that Springboard does.
        // Appears as if pwPrompt just pops up on the background as if it was Springboard's transition from the beginning. very sweet.
        UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
        CGPoint p;
        p.x = 0.0f; p.y = -20.0f; // TODO get exact size of status bar
        [background setOrigin:p];
        
        if (state == LBPP_AUTH) {
            _pwPrompt = [[LockboxPwPromptView alloc] initWithFrame:CGRectMake(LBPP_LEFT, LBPP_TOP_AUTH, LBPP_WIDTH, LBPP_HEIGHT_AUTH) promptState:LBPP_AUTH];
        }
        else {
            _pwPrompt = [[LockboxPwPromptView alloc] initWithFrame:CGRectMake(LBPP_LEFT, LBPP_TOP_NEW, LBPP_WIDTH, LBPP_HEIGHT_NEW) promptState:state];
        }
        [_pwPrompt setDelegate:self];
        
        struct CGSize kbSize = [UIKeyboard defaultSize];
        UIKeyboard* keyboard = [[UIKeyboard alloc] initWithFrame:CGRectMake(frame.origin.x, frame.size.height - kbSize.height, kbSize.width, kbSize.height)];
        [[keyboard defaultTextTraits] setAutoCapsType: 0];

        [self addSubview:background];
        [self addSubview:keyboard];
        [self addSubview:_pwPrompt];
        
        CFRelease(colorSpace);
    }
    
    return self;
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button
{
    [sheet dismiss];
    [_pwPrompt clearFields];
}

- (void)passwordEntry:(NSString*)password
{
    NSLog(@"passwordEntry received.");
    if (true /*state == LBPP_AUTH || state == LBPP_NEW*/) {
        @try {
            [_storage loadWithKey:password];
            [_delegate pwViewFinished];
        }
        @catch (NSException* ex) {
            NSLog(@"***Caught exception during loadWithKey: %@ reason: %@", [ex name], [ex reason]);
            if ([ex name] == LockboxInvalidKeyException) {
                UIAlertSheet *infoSheet = [[UIAlertSheet alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
                [infoSheet setBodyText:@"Invalid Password"];
                [infoSheet addButtonWithTitle:@"OK"];
                [infoSheet setDelegate:self];
                [infoSheet setAlertSheetStyle:0];
                [infoSheet presentSheetFromAboveView:self];
            }
        }
    }
    else {
        // Password change not implemented yet!
    }

}

- (void)clearFields
{
    [_pwPrompt clearFields];
}


/*- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
     NSLog(@"LBPasswordView: Requested method for selector: %@", NSStringFromSelector(selector));
     return [super methodSignatureForSelector:selector];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    NSLog(@"LBPasswordView: Request for selector: %@", NSStringFromSelector(aSelector));
    return [super respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
     NSLog(@"LBPasswordView: Called from: %@", NSStringFromSelector([anInvocation selector]));
    [super forwardInvocation:anInvocation];
}*/

@end
